package com.blt.govdata.utils;

import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author jiangzeyin
 * @create 2017 02 14 12:44
 */
@Configuration
public class ServiceInfoUtil implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
    private static EmbeddedServletContainerInitializedEvent event;

    public static int getPort() {
        Assert.notNull(event);
        int port = event.getEmbeddedServletContainer( ).getPort( );
        Assert.state(port != -1, "端口号获取失败");
        return port;
    }

    public static String getHost() {
        String host = null;
        try {
            host = InetAddress.getLocalHost( ).getHostAddress( );
        } catch (UnknownHostException e) {
            e.printStackTrace( );
        }
        Assert.state(host != null, "IP获取失败");
        return host;
    }

    @Override
    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
        ServiceInfoUtil.event = event;
    }
}



